﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrumDashboard.Library.Models;

namespace ScrumDashboardMvc.Models
{
    public class OverviewViewData
    {
        private OverviewViewData()
        {
        }

        public static OverviewViewData Create(TeamProject proj, SprintViewData model)
        {
            OverviewViewData stats = new OverviewViewData();

            stats.ReleaseWorkStreamIterationPath = model.Sprint.IterationPath.Substring(0,model.Sprint.IterationPath.LastIndexOf(@"\"));
            stats.SprintTeamIterationPath = model.TeamSprint != null ? model.TeamSprint.IterationPath : model.Sprint.IterationPath;

            stats.BurndownChart = proj.BurndownChart + @"&rs:ClearSession=true&rs:Command=RenderStream&rc:LinkTarget=main&rs:Format=HTML4.0&pReleaseWorkStreamPath=" +
                                    HttpUtility.UrlEncode(@"\" + stats.ReleaseWorkStreamIterationPath + @"\%") + 
                                    "&pSprintTeam=" + HttpUtility.UrlEncode(@"\" + stats.SprintTeamIterationPath + (model.TeamSprint==null ?  @"\%" : ""));

            stats.ReportingUrl = proj.ReportingUrl;

            stats.SprintStart = model.Sprint.SprintStart;
            stats.SprintEnd = model.Sprint.SprintEnd;

            foreach (ProductBacklogItem pbi in model.ProductBacklog)
            {
                stats.TotalStoryPoints += pbi.StoryPoints;
                if (pbi.InState("Done"))
                    stats.CompletedStoryPoints += pbi.StoryPoints;
            }

            if (stats.TotalStoryPoints > 0)
                stats.CompletedStoryPointsPercent = 100 * stats.CompletedStoryPoints / stats.TotalStoryPoints;
            else
                stats.CompletedStoryPointsPercent = 0;

            stats.WorkTotal = 0;
            stats.WorkRemaining = 0;

            foreach (ProductBacklogItem pbi in model.ProductBacklog)
            {
                foreach (SprintBacklogTask sbi in pbi.SprintBacklog)
                {
                    stats.WorkTotal += sbi.EstimatedEffort.HasValue ? sbi.EstimatedEffort.Value : 0;
                    
                    if (sbi.IsUnplanned(model.Sprint.SprintStart))
                    {
                        stats.ItemsUnplannedWork += sbi.EstimatedEffort.HasValue ? sbi.EstimatedEffort.Value : 0;
                    }

                    switch (sbi.State.ToLower())
                    {
                        case "done":
                            stats.ItemsDone += sbi.EstimatedEffort.HasValue ? sbi.EstimatedEffort.Value : 0;
                            break;
                        case "in progress":
                            stats.ItemsInProgress += sbi.WorkRemaining;
                            break;
                        case "not started":
                            stats.ItemsNotStarted += sbi.EstimatedEffort.HasValue ? sbi.EstimatedEffort.Value : 0;
                            break;
                    }
                }
            }

            stats.WorkRemaining = stats.ItemsInProgress + stats.ItemsNotStarted;
            stats.CompletedWork = stats.WorkTotal - stats.WorkRemaining;

            if (stats.WorkTotal > 0)
                stats.CompletedWorkPercent = 100 * stats.CompletedWork / stats.WorkTotal;
            else
                stats.CompletedWorkPercent = 0;

            return stats;
        }

        public string BurndownChart { get; set; }
        public string ReportingUrl { get; set; }

        public DateTime SprintStart;
        public DateTime SprintEnd;

        public double CompletedWork;
        public double CompletedWorkPercent;
        public double WorkTotal;
        public double WorkRemaining;

        public double TotalStoryPoints;
        public double CompletedStoryPoints;
        public double CompletedStoryPointsPercent;

        public double ItemsDone;
        public double ItemsNotStarted;
        public double ItemsReadyForTest;
        public double ItemsInProgress;
        public double ItemsUnplannedWork;

        public string SprintTeamIterationPath { get; set; }

        public string ReleaseWorkStreamIterationPath { get; set; }
    }
}